package com.toonly.defaultvalues;

import com.toonly.defaultvalues.framework.*;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

public class ListDefaultValueGenerator extends AChainedDefaultValueGenerator<Main.Context> {

    @Override
    public IGeneratedValue generate() {
        if (this.source.getClazz() == List.class) {

            Type type = this.source.getType();
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;

                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

                if (actualTypeArguments.length > 0) {
                    Type argType = actualTypeArguments[0];

                    this.set(new Main.Context(Object.class, argType));
                    IGeneratedValue generate = super.generate();

                    if (generate.suc()) {
                        return new GeneratedValue(true, Collections.singletonList(generate.val()));
                    }
                }
            }
        }
        return new GeneratedValue(false, null);
    }

}
